package com.ironman.compute.generator;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.ironman.event.structure.FixedSizeLinkedBlockingDeque;
import com.ironman.event.properties.SysTrainingProperties;
import lombok.RequiredArgsConstructor;
import org.noear.snack.ONode;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Component
@RequiredArgsConstructor
public class GpsComputeDataGenerator extends ComputeDataGenerator {

    final SysTrainingProperties sysTrainingProperties;

    FixedSizeLinkedBlockingDeque<String> blockingDeque = new FixedSizeLinkedBlockingDeque<>(300);

    @Override
    public String topicType() {
        return "gps";
    }

    @Override
    public ONode generate() {
        List<String> recentGps = blockingDeque.all();
        Collections.reverse(recentGps);
        return ONode.newObject()
                .set("mHmName", sysTrainingProperties.getMHmName())
                .set("currentGps", gps())
                .set("recentGps", recentGps);
    }

    public String gps() {
        String gpsData = StrUtil.format("{},{},{}",
                NumberUtil.round(RandomUtil.randomDouble(121.005572, 121.010572), 6).doubleValue(),
                NumberUtil.round(RandomUtil.randomDouble(31.102556, 31.128556), 6).doubleValue(),
                DateUtil.format(new Date(), "HHmmss")
        );
        blockingDeque.offerLast(gpsData);
        return gpsData;
    }

}
